﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SARControlLib
{
	/// <summary>
	/// SARScrollBar.xaml 的交互逻辑
	/// </summary>
	public partial class SARScrollBar : UserControl
	{
		public SARScrollBar()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(SARScrollBar_Loaded);
		}

        void SARScrollBar_Loaded(object sender, RoutedEventArgs e)
        {
            SARRadio = radio;
        }

        private double radio = 0;

        public double SARRadio
        {
            get
            {
                return radio;
            }

            set
            {
                if (value < 0)
                {
                    value = 0;
                }
                else if (value > 1)
                {
                    value = 1;
                }

                
                if (SARScrollBarDragged != null)
                {
                    SARScrollBarDragged.Invoke(value);                        
                }

                this.radio = value;
                this.gridIcon.Margin = new Thickness(0, value * (this.ActualHeight - 23), 0, 0);
            }
        }

        public event ScrollBarDraggedHandler SARScrollBarDragged;

        private Point startPoint;
        private bool dragging = false;

        private void gridIcon_MouseMove(object sender, MouseEventArgs e)
        {            
            if (e.LeftButton == MouseButtonState.Pressed && dragging)
            {
                Point nowPoint = e.GetPosition(this);

                double delta = nowPoint.Y - startPoint.Y;
                double margintop = this.gridIcon.Margin.Top + delta;
                if (margintop < 0)
                {
                    margintop = 0;
                }
                else if (margintop > this.ActualHeight - 23)
                {
                    margintop = this.ActualHeight - 23;
                }

                this.gridIcon.Margin = new Thickness(0, margintop, 0, 0);
                this.SARRadio = margintop / (this.ActualHeight - 23);
                startPoint = nowPoint;

            }
        }

        private void gridIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition(this);
            dragging = true;
        }

        private void gridIcon_MouseLeave(object sender, MouseEventArgs e)
        {
            dragging = false;
        }

        private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == true)
            {
                this.Opacity = 1;
            }
            else
            {
                this.Opacity = 0.5;
            }
        }

        private void gridIcon_LostMouseCapture(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && dragging)
            {
                Point nowPoint = e.GetPosition(this);

                double delta = nowPoint.Y - startPoint.Y;
                double margintop = this.gridIcon.Margin.Top + delta;
                if (margintop < 0)
                {
                    margintop = 0;
                }
                else if (margintop > this.ActualHeight - 23)
                {
                    margintop = this.ActualHeight - 23;
                }

                this.gridIcon.Margin = new Thickness(0, margintop, 0, 0);
                this.SARRadio = margintop / (this.ActualHeight - 23);
                startPoint = nowPoint;
            }
        }
             
	}

    public delegate void ScrollBarDraggedHandler(double radio);

}